<script setup lang="ts">
import { ref } from 'vue';
import { getScanAPI } from '@/services/hot';

const { safeAreaInsets } = uni.getSystemInfoSync();
// const scannedPhone = ref(''); // 保存扫描到的手机号
const backendResponse = ref(''); // 保存后端返回的信息

const startScan = async () => {
  try {
    const res = await wx.scanCode();
        uni.showModal({
          title: '扫描结果',
          content: `确认绑定此设备id：${res.result}`,
          showCancel: false,
        });

    // 发送扫描到的数据到后端API
    const response = await getScanAPI(res.result);

    // 更新数据
    // scannedPhone.value = res.result;
    backendResponse.value = response;
    // this.$refs.someRef.updateData(response);
  } catch (error) {
    console.error("扫描失败：", error);
  }
};
</script>

<template>
  <view class="navbar" :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
    <!-- logo文字 -->
    <view class="logo">
<!-- 渲染扫描到的信息 -->
    <view v-if="scannedPhone">{{ scannedPhone }}</view>

    <!-- 渲染后端返回的信息 -->
    <view v-if="backendResponse">{{ backendResponse }}</view>
    </view>
    <!-- 搜索条 -->
    <view class="search">
      <text class="icon-search">搜索</text>
      <text class="icon-scan" @tap="startScan"></text>
    </view>
  </view>
</template>

<style lang="scss">
/* 自定义导航条 */
.navbar {
  // background-image: url(@/static/images/navigator_bg.png);
  background-color: #A5DD9B;
  background-size: cover;
  position: relative;
  display: flex;
  flex-direction: column;
  padding-top: 20px;

  .logo {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 30rpx;
    padding-top: 20rpx;

    .logo-image {
      width: 166rpx;
      height: 39rpx;
    }

    .logo-text {
      flex: 1;
      line-height: 28rpx;
      color: #fff;
      margin: 2rpx 0 0 20rpx;
      padding-left: 20rpx;
      border-left: 1rpx solid #fff;
      font-size: 26rpx;
    }
  }

  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10rpx 0 26rpx;
    height: 64rpx;
    margin: 16rpx 20rpx;
    color: #fff;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: rgba(255, 255, 255, 0.5);
  }

  .icon-search {
    &::before {
      margin-right: 10rpx;
    }
  }

  .icon-scan {
    font-size: 30rpx;
    padding: 15rpx;
  }
}
</style>
